package com.zlt.bkparking.controller.parking;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zlt.bkparking.dto.AppUserDTO;
import com.zlt.bkparking.service.UserService;
import com.zlt.bkparking.vo.AppUserVO;
import com.zlt.bkparking.vo.Result;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import java.util.List;

/**
 * @Author IBing
 * @Date 2022/10/2 19:51
 * @Version 1.0
 */
@RestController
@RequestMapping("user")
@Slf4j
public class AppUserController {
    @Autowired
    private UserService userService;


    /**
     * 查询停车场的所有用户
     * @param appUserDTO
     * @return
     */
    @PostMapping("users")
    public Result selectUser(@ApiParam AppUserDTO appUserDTO){
        log.info("提交的数据{}",appUserDTO);
        try {

            PageInfo<AppUserVO> pageInfo = userService.selectUsers(appUserDTO);
            return Result.ok("查询成功",pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("查询失败："+e.getMessage());
        }
    }
    @PostMapping("insertUser")
    public Result insertUser( AppUserDTO appUserDTO){
        log.debug("insertUser提交的数据{}",appUserDTO);
        try {
            boolean success = userService.insertUser(appUserDTO);
            return success ? Result.ok("插入成功") : Result.error("插入失败") ;
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("插入失败："+e.getMessage());
        }
    }

    /**
     * 更改用户信息
     * @param appUserDTO
     * @return
     */
    @PutMapping("users")
    public Result updateUser( AppUserDTO appUserDTO){
        log.info("updateUser的数据{}",appUserDTO);
        try {
            boolean success = userService.updateUser(appUserDTO);
            return success ? Result.ok("更新成功") : Result.error("更新失败") ;
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("更新失败："+e.getMessage());
        }
    }
    /**
     * 注销
     * @param auId
     * @return
     */
    @DeleteMapping("users")
    public Result deleteUser(String auId){
        log.debug("updateUser的数据{}",auId);
        try {
            boolean success = userService.deleteUser(auId);
            return success ? Result.ok("删除成功") : Result.error("删除失败") ;
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("删除失败："+e.getMessage());
        }
    }





    /**
     * 查询会员用户
     * @param appUserDTO
     * @return
     */
    @PostMapping("vipUser")
    public Result selectVipUser(@ApiParam AppUserDTO appUserDTO){
        log.debug("提交的数据{}",appUserDTO);
        try {
            PageInfo<AppUserVO> pageInfo=userService.selectVipUser(appUserDTO);
            return Result.ok("查询成功",pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("查询失败："+e.getMessage());
        }
    }
    /**
     * 获取非 平台 Vip用户
     * @return
     */
    @GetMapping("vipUser")
    public Result getNotVipUser(){
        List<AppUserVO> userVOS = userService.getNotVipUser(0);
        return Result.ok("查询成功",userVOS);
    }
    /**
     * 添加平台VIP
     * @param appUserDTO
     * @return
     */
    @PostMapping("insertVip")
    public Result insertVip( AppUserDTO appUserDTO){
        log.info("insertUser提交的数据{}",appUserDTO);
        try {
            boolean success = userService.insertVip(appUserDTO,0);
            return success ? Result.ok("插入成功") : Result.error("插入失败") ;
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("插入失败："+e.getMessage());
        }
    }
    /**
     * 平台VIP充值
     * @param appUserDTO
     * @return
     */
    @PutMapping("vipUser")
    public Result rechargeVIP( AppUserDTO appUserDTO){
        log.info("rechargeVIP的数据{}",appUserDTO);
        try {
            boolean success = userService.rechargeVIP(appUserDTO,1);
            return success ? Result.ok("充值成功") : Result.error("充值失败") ;
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("充值失败："+e.getMessage());
        }
    }

    /**
     * 删除平台过期VIP
     * @param auId
     * @return
     */
    @DeleteMapping("vipUser")
    public Result deleteVip( String auId){
        log.info("deleteParkVip的数据{}",auId);
        try {
            boolean success = userService.deleteVip(auId);
            return success ? Result.ok("删除成功") : Result.error("删除失败") ;
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("删除失败："+e.getMessage());
        }
    }



    /**
     * 查询停车场VIP
     * @param appUserDTO
     * @return
     */
    @PostMapping("parkVip")
    public Result selectParkVip(@ApiParam AppUserDTO appUserDTO){
        log.debug("提交的数据{}",appUserDTO);
        try {
            PageInfo<AppUserVO> pageInfo = userService.selectParkVipUser(appUserDTO);
            return Result.ok("查询成功",pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("查询失败："+e.getMessage());
        }
    }

    /**
     * 获取非停车场VIP用户
     * @return
     */
    @GetMapping("parkVip")
    public Result getNotParkVipUser(){
        List<AppUserVO> userVOS = userService.getNotVipUser(1);
        return Result.ok("查询成功",userVOS);
    }

    /**
     * 添加停车场会员
     * @param appUserDTO
     * @return
     */
    @PostMapping("insertParkVip")
    public Result insertParkVip( AppUserDTO appUserDTO){
        log.info("insertUser提交的数据{}",appUserDTO);
        try {
            boolean success = userService.insertVip(appUserDTO,1);
            return success ? Result.ok("插入成功") : Result.error("插入失败") ;
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("插入失败："+e.getMessage());
        }
    }

    /**
     * 停车场VIP充值
     * @param appUserDTO
     * @return
     */
    @PutMapping("parkVip")
    public Result rechargeParkVIP( AppUserDTO appUserDTO){
        log.info("rechargeVIP的数据{}",appUserDTO);
        try {
            boolean success = userService.rechargeVIP(appUserDTO,2);
            return success ? Result.ok("充值成功") : Result.error("充值失败") ;
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("充值失败："+e.getMessage());
        }
    }


//    /**
//     * 编辑停车场会员
//     * @param appUserDTO
//     * @return
//     */
//    @PutMapping("parkVip")
//    public Result updateParkVip( AppUserDTO appUserDTO){
//        log.info("updateUser的数据{}",appUserDTO);
//        try {
//            boolean success = userService.updateParkVip(appUserDTO);
//            return success ? Result.ok("更新成功") : Result.error("更新失败") ;
//        } catch (Exception e) {
//            e.printStackTrace();
//            return Result.error("更新失败："+e.getMessage());
//        }
//    }

    /**
     * 删除停车场 过期 VIP用户
     * @param auId
     * @return
     */
    @DeleteMapping("parkVip")
    public Result deleteParkVip( String auId){
        log.info("deleteParkVip的数据{}",auId);
        try {
            boolean success = userService.deleteParkVip(auId);
            return success ? Result.ok("删除成功") : Result.error("删除失败") ;
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("删除失败："+e.getMessage());
        }
    }

    /**
     * 批量取消停车场vip 或删除用户
     * @param auIds
     * @return
     */
    @DeleteMapping("batchDeleteParkVip")
    public Result batchDeleteParkVip( @RequestParam("auIds")List<String> auIds,@RequestParam("state")Integer state){
        log.info("batchDeleteParkVip的数据:{}",auIds);
        try {
            boolean success = userService.batchDeleteParkVip(auIds,state);
            return success ? Result.ok("删除成功") : Result.error("删除失败") ;
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("删除失败："+e.getMessage());
        }
    }




}
